import { createRouter, createWebHistory } from 'vue-router'
import home from '../views/zs-home.vue'
import { getUserInfo } from "../api/user"
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'home',
      component: home,
      children:[
        {
          path:'/',
          name:'worktable',
          component: () => import('../dashboard/zs-worktable/zs-worktable.vue')
        },
        {
          path: '/build',
          name: 'build',
          component: () => import('../dashboard/zs-pack/zs-build.vue')
        },
        {
          path: '/company',
          name: 'company',
          component: () => import('../dashboard/zs-pack/zs-company.vue')
        },
        {
          path: '/area',
          name: 'area',
          component: () => import('../dashboard/zs-driving/zs-area.vue')
        },
        {
          path: '/monthCard',
          name: 'monthCard',
          component: () => import('../dashboard/zs-driving/zs-monthCard.vue'),

        },
        {
          path: '/payment',
          name: 'payment',
          component: () => import('../dashboard/zs-driving/zs-payment.vue')
        },
        {
          path: '/accounting',
          name: 'accounting',
          component: () => import('../dashboard/zs-driving/zs-accounting.vue')
        },
        {
          path: '/property',
          name: 'property',
          component: () => import('../dashboard/zs-property/zs-property.vue')
        },
        {
          path: '/pieceSwing',
          name: 'pieceSwing',
          component: () => import('../dashboard/zs-pieceSwing/zs-pieceSwing.vue')
        },
        {
          path: '/waring',
          name: 'waring',
          component: () => import('../dashboard/zs-pieceSwing/zs-waring.vue')
        },
        {
          path: '/employees',
          name: 'employees',
          component: () => import('../dashboard/zs-system/zs-employees.vue')
        },
        {
          path: '/role',
          name: 'role',
          component: () => import('../dashboard/zs-system/zs-role.vue')
        },
        
      ]
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('../views/zs-login.vue')
    },
    {
      path: '/monthCard/addMonthCard',
      name: 'addMonthCard',
      component: () => import('../dashboard/zs-driving/components/setMonthCard.vue')
    },
    {
      path: '/monthCard/renewMonthCard/:id',
      name: 'renewMonthCard',
      component: () => import('../dashboard/zs-driving/components/setMonthCard.vue')
    },
    {
      path: '/monthCard/viewMonthCard/:id',
      name: 'viewMonthCard',
      component: () => import('../dashboard/zs-driving/components/setMonthCard.vue')
    },
    {
      path: '/monthCard/editMonthCard/:id',
      name: 'editMonthCard',
      component: () => import('../dashboard/zs-driving/components/setMonthCard.vue')
    },
    {
      path: '/company/enterprise/:id?/:name?',
      name: 'enterprise',
      component: () => import('../dashboard/zs-pack/components/setEnterprisr.vue')
    },
  ]
})

const whilteList = ['/login']
import store from "../stores/counter"
router.beforeEach( async(to,from,next)=>{
  const counter = store()
  // console.log(counter.token);
  if(counter.token){
    await getUserInfo()
    // console.log(res);
    next()
  }else{
    if(whilteList.includes(to.path)){
      next()
    }
    next('/login')
  }
})

export default router
